﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Thaire.Investment.Common;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class SearchFixedIncomeSecuritiesModel
    {
        public SearchFixedIncomeSecuritiesModel()
        {
            CompanyList = new BindingList<SelectListItem>();
            IssuerList = new BindingList<SelectListItem>();
            IssuerTypeList = new BindingList<SelectListItem>();

          
          //  AssignDefaultData();

        }

        public void ClearFilterAndData()
        {

                WebUtil.ClearSelectionDropDown(CompanyList);
                WebUtil.ClearSelectionDropDown(IssuerList);
                WebUtil.ClearSelectionDropDown(IssuerTypeList);
                if (Result != null)
                    Result.Clear();
                SelectedCompany = null;
                SelectedIssuer = null;
                SelectedIssuerType = null;
                Symbol = null;
                IssueTerm = null;
        }

        public SearchFiexdIncomeFilter ToSearchFiexdIncomeFilter()
        {
            SearchFiexdIncomeFilter filter = new SearchFiexdIncomeFilter();

            filter.Company = SelectedCompany;
            filter.Issuer = SelectedIssuer;
            filter.IssuerType = SelectedIssuerType;
            filter.Symbol = Symbol;


            filter.IssueTerm = DataConverter.ConvertStringToDecimalNull(IssueTerm);
            return filter;
        }

        public static SearchFixedIncomeSecuritiesModel BuildFixedIncomeModel(FixedIncomeListData data)
        {
            SearchFixedIncomeSecuritiesModel model = new SearchFixedIncomeSecuritiesModel();
          
            model.CompanyList.Add(WebUtil.SelectListSelectDefault());
            model.IssuerList.Add(WebUtil.SelectListAllDefault());
          
            foreach (var item in data.Company)
            {
                model.CompanyList.Add(new SelectListItem() { Text = item.CompanyNameTh, Value = item.CompanyId.ToString() });
            }
            foreach (var item in data.Issuer)
            {
                model.IssuerList.Add(new SelectListItem() { Text = item.IssuerName, Value = item.IssuerId.ToString() });
            }
            foreach (var item in data.IssuerType)
            {
                model.IssuerTypeList.Add(new SelectListItem() { Text = item.ValueTH, Value = item.ID.ToString() });
            }
    
            model.IssuerTypeList.Insert(0,WebUtil.SelectListAllDefault());
            return model;
        }

        //private void AssignDefaultData()
        //{
        //    var defaultItem = new SelectListItem() { Selected = true, Text = "--Select data--", Value = "" };
        //    CompanyList.Add(defaultItem);
        //    IssuerList.Add(defaultItem);
        //    IssuerTypeList.Add(defaultItem);

        
        //    CompanyList.Add(item: new SelectListItem() { Selected = false, Text = "Thaire Life", Value = "1" });
        //    CompanyList.Add(item: new SelectListItem() { Selected = false, Text = "Thaire Training", Value = "2" });
        //    CompanyList.Add(item: new SelectListItem() { Selected = false, Text = "Thaire Service", Value = "3" });

        //    IssuerList.Add(item: new SelectListItem() { Selected = false, Text = "รัฐบาลไทย", Value = "1" });
        //    IssuerList.Add(item: new SelectListItem() { Selected = false, Text = "ธนาคารแห่งประเทศไทย", Value = "2" });
        //    IssuerList.Add(item: new SelectListItem() { Selected = false, Text = "กระทรวงการคลัง", Value = "3" });

        //    IssuerTypeList.Add(item: new SelectListItem() { Selected = false, Text = "รัฐบาล (MoF)", Value = "1" });
        //    IssuerTypeList.Add(item: new SelectListItem() { Selected = false, Text = "รัฐวิสาหกิจ (SOE)", Value = "2" });
        //    IssuerTypeList.Add(item: new SelectListItem() { Selected = false, Text = "รัฐวิสาหกิจ (SOEG)", Value = "3" });
        //    IssuerTypeList.Add(item: new SelectListItem() { Selected = false, Text = "Treasury Bill ตั๋วเงินคลัง", Value = "4" });
        //    IssuerTypeList.Add(item: new SelectListItem() { Selected = false, Text = "เอกชน", Value = "5" });
        //    //end 
        //}
        [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedCompany { get; set; }
        public IList<SelectListItem> CompanyList { get; set; }
        public int? SelectedIssuer { get; set; }
        public IList<SelectListItem> IssuerList { get; set; }
        public int? SelectedIssuerType { get; set; }
        public IList<SelectListItem> IssuerTypeList { get; set; }
        public string Symbol { get; set; }




        [Range((double)0, double.MaxValue, ErrorMessage = "ข้อมูลต้องเป็นตัวเลข")]
        public String IssueTerm { get; set; }

        public IList<FixIncomeSecuritiesHeader> Result { get; set; }

        public String SearchBtn { get; set; }
        public String ResetBtn { get; set; }


    }
}